package javabase;

import java.util.LinkedList;

/**
 * Created by mHandler on 2018/5/15 0015.
 * Description:基于LinkedList的栈实现
 */
public class LinkedListToStack<T> {
    private LinkedList<T> stack = new LinkedList<T>();

    /** 入栈 */
    public void push(T v) {
        stack.addFirst(v);
    }

    /** 出栈，但不删除 */
    public T peek() {
        return stack.getFirst();
    }

    /** 出栈，删除 */
    public T pop() {
        return stack.removeFirst();
    }

    /** 栈是否为空 */
    public boolean empty() {
        return stack.isEmpty();
    }

    /** 打印栈元素 */
    public String toString() {
        return stack.toString();
    }

    public static void main(String[] args) {
        LinkedListToStack<String> stack = new LinkedListToStack<>();
        stack.push("a");
        stack.push("b");
        stack.push("c");
        //[c, b, a]
        System.out.println(stack.toString());
        //c--[c, b, a]
        Object obj=stack.peek();
        System.out.println(obj+"--"+stack.toString());
        obj=stack.pop();
        //c--[b, a]
        System.out.println(obj+"--"+stack.toString());
        //false
        System.out.println(stack.empty());
    }
}
